package cn.tedu._03vehicle.controller;

import cn.tedu._03vehicle.base.response.JsonResult;
import cn.tedu._03vehicle.base.response.StatusCode;
import cn.tedu._03vehicle.mapper.VehicleMapper;
import cn.tedu._03vehicle.pojo.dto.VehicleAddParam;
import cn.tedu._03vehicle.pojo.dto.VehicleListQuery;
import cn.tedu._03vehicle.pojo.entity.Vehicle;
import cn.tedu._03vehicle.pojo.vo.UserLoginVO;
import cn.tedu._03vehicle.pojo.vo.VehicleListVO;
import com.github.xiaoymin.knife4j.annotations.ApiOperationSupport;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.Parameter;
import io.swagger.v3.oas.annotations.Parameters;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.servlet.http.HttpSession;
import lombok.extern.slf4j.Slf4j;
import org.hibernate.validator.constraints.Range;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

import java.util.Date;
import java.util.List;

@RestController
@RequestMapping("/v1/vehicle/")
@Slf4j
@Tag(name = "02-车辆管理模块")
@Validated
public class VehicleController {
    @Autowired
    private VehicleMapper vehicleMapper;

    @Operation(summary = "添加车辆")
    @ApiOperationSupport(order = 10)
    @PostMapping("add")
    public JsonResult add(@RequestBody VehicleAddParam vehicleAddParam, HttpSession session){
        log.debug("VehicleAddParam:" + vehicleAddParam);
        UserLoginVO userLoginVO = (UserLoginVO) session.getAttribute("user");
      /*  if (userLoginVO == null) return new JsonResult(StatusCode.NOT_LOGIN);*/
        Vehicle vehicle = new Vehicle();
        BeanUtils.copyProperties(vehicleAddParam, vehicle);
        vehicle.setUserId(userLoginVO.getId());
        vehicle.setCreateTime(new Date());
        vehicle.setUpdateTime(new Date());
        int count = vehicleMapper.insertVehicle(vehicle);
        if (count>0)return JsonResult.ok();
        return new JsonResult(StatusCode.OPERATION_FAILED);
    }
    @GetMapping("list")
    @Operation(summary = "车辆列表")
    @ApiOperationSupport(order = 20)
    @Parameters(value = {
            @Parameter(name = "brand" ,description = "车辆品牌"),
            @Parameter(name = "license",description = "车牌号"),
            @Parameter(name = "vehicleListQuery",hidden = true)
    })
    public JsonResult list(VehicleListQuery vehicleListQuery, HttpSession session){
        UserLoginVO userLoginVO = (UserLoginVO) session.getAttribute("user");
        /*if (userLoginVO == null) return new JsonResult(StatusCode.NOT_LOGIN);*/
        List<VehicleListVO> vehicleListVO = vehicleMapper.selectVehicleListVO(vehicleListQuery,userLoginVO.getId());
        return JsonResult.ok(vehicleListVO);
    }
    @GetMapping("del/{id}")
    @Operation(summary = "删除车辆")
    @ApiOperationSupport(order = 30)
    public JsonResult del(@Range(min = 100 ,max = 500,message = "车辆编号必须在100到500之间") @PathVariable Long id, HttpSession session){
        /*if (id<0){
            throw new IllegalArgumentException("车辆ID不能小于0");
        }*/
        UserLoginVO userLoginVO = (UserLoginVO) session.getAttribute("user");
        /*if (userLoginVO == null) return new JsonResult(StatusCode.NOT_LOGIN);*/
        int count = vehicleMapper.deleteVehicleById(id, userLoginVO.getId());
        if (count>0)return JsonResult.ok();
        return new JsonResult(StatusCode.OPERATION_FAILED);
    }
}
